#pragma once

#include "log.hpp"
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>

using namespace std;

const char *pathname = "/home/Lkmb";
const int proj_id = 0x6666;

const int size = 4096;
Log log;

key_t getkey()
{
    key_t k = ftok(pathname, proj_id);
    if (k < 0)
    {
        log(Fatal, "ftok error:%s", strerror(errno));
        exit(1);
    }
    return k;
}

int getsharedmem(int flag)
{
    key_t k = getkey();
    int shmid = shmget(k, size, flag);
    if (shmid < 0)
    {
        log(Fatal, "shmget error:%s", strerror(errno));
        exit(2);
    }

    return shmid;
}

int creatshm()
{
    return getsharedmem(IPC_CREAT | IPC_EXCL | 0666);
}

int getshm()
{
    return getsharedmem(IPC_CREAT);
}



#define SIZE 1024

#define PATH "./myfifo"
#define MODE 0666

enum
{
    FIFO_CREATE_ERR = 1,
    FIFO_UNLINK_ERR,
    FIFO_OPEN_ERR
};



class Init
{
public:

    Init()
    {
        // 建立管道
        int n = mkfifo(PATH, MODE);
        if (n < 0)
        {
            perror("mkfifo");
            exit(FIFO_CREATE_ERR);
        }
    }

    ~Init()
    {
        // 释放管道
        int m = unlink(PATH);
        if (m < 0)
        {
            perror("unlink");
            exit(FIFO_UNLINK_ERR);
        }
    }

};